//
//  MenuVC.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/8/15.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit
enum PresentVC {
    case none
    //case loginVC
    case userProfileVC
    case memberDetailVC
    case storeDetailVC
    /// 记录
    case rukuRecordVC
    case saleRecordVC
    case suobanRecordVC
    case dingyueRecordVC
    case shoppingListVC
    
    case shouxinVC
    case salerShouXinRecordVC
    case buyerShouXinRecordVC
    
    case indebtRecordVC
}

class MenuVC: UITableViewController {
    
    var presentVC: PresentVC = .none
    var userProfileVC: UserProfileVC!
    var memberDetailVC: MemberDetailVC!
    var storeDetailVC: StoreDetailVC!
    var rukuRecordVC: RuKuRecordVC!
    var saleRecordVC: SaleRecordVC!
    var suobanRecordVC: SuoBanVC!
    var dingyueRecordVC: DingYueVC!
    var shoppingListVC: ShoppingListVC!
    
    var shouxinVC: ShouXinVC!
    var shouxinRecordVC: ShouXinRecordVC!
    
    var indebtRecordVC: IndebtRecordVC!
    
    lazy var mainStoryboard: UIStoryboard = UIStoryboard(name: "MainStoryboard", bundle: nil)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.revealViewController().delegate = self
        tableView.register(UINib.init(nibName: "MenuCell", bundle: nil), forCellReuseIdentifier: "TopMenuCell")
        tableView.backgroundColor = #colorLiteral(red: 0.9369913936, green: 0.9376766086, blue: 0.9578072429, alpha: 1)
    }
    
    fileprivate let menuOptions = [[""], ["销售清单"], ["商店管理", "成员管理"], ["入库记录", "销售记录", "索版记录", "订阅记录"], ["授信二维码", "商户授信记录管理", "买家授信记录管理","欠款记录管理"]]
    fileprivate let titleOptions = [nil, "销售", "管理", "记录", "授信"]
    

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let indexPath = IndexPath(row: 0, section: 0)
        let cell = self.tableView.cellForRow(at: indexPath) as! MenuCell
        cell.label.text = User.userName
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 && indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TopMenuCell") as! MenuCell
            if User.isLogin {
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                let filePath = documentsURL.appendingPathComponent("icon.png").path
                cell.label.text = User.userName
                cell.avargView.contentMode = .scaleAspectFit
                cell.backgroundColor = #colorLiteral(red: 0.9369913936, green: 0.9376766086, blue: 0.9578072429, alpha: 1)
                if FileManager.default.fileExists(atPath: filePath) {
                    cell.avargView.image = UIImage(contentsOfFile: filePath)
                } else {
                    cell.avargView.image = #imageLiteral(resourceName: "user")
                }
                return cell
            }
            cell.backgroundColor = #colorLiteral(red: 0.9369913936, green: 0.9376766086, blue: 0.9578072429, alpha: 1)
            cell.label.text = "请登录"
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell")!
        cell.textLabel?.text = menuOptions[indexPath.section][indexPath.row]
        cell.backgroundColor = #colorLiteral(red: 0.9369913936, green: 0.9376766086, blue: 0.9578072429, alpha: 1)
        return cell
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return titleOptions.count
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuOptions[section].count
    }
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        return titleOptions[section]
    }

    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.section == 0 {
            if !User.isLogin { //User didn't login
                let loginVC = LoginController()
                let navigationController = UINavigationController(rootViewController: loginVC)
                self.present(navigationController, animated: true, completion: nil)
            } else {
                userProfileVC = mainStoryboard.instantiateViewController(withIdentifier: "UserProfileNav") as! UserProfileVC
                userProfileVC.delegate = self
                //let navigationController = UINavigationController(rootViewController: userProfileVC)
                presentVC = .userProfileVC
                self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
            }
            
        } else if indexPath.section == 1 && indexPath.row == 0 {
            presentVC = .shoppingListVC
            shoppingListVC = ShoppingListVC()
            self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
        } else if indexPath.section == 2 && indexPath.row == 0 {
            presentVC = .storeDetailVC
            storeDetailVC = StoreDetailVC()
            self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
        } else if indexPath.section == 2 && indexPath.row == 1 {
            presentVC = .memberDetailVC
            memberDetailVC = MemberDetailVC()
            self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
        } else if indexPath.section == 3 && indexPath.row == 0 {
            presentVC = .rukuRecordVC
            rukuRecordVC = RuKuRecordVC()
            self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
        } else if indexPath.section == 3 && indexPath.row == 1 {
            presentVC = .saleRecordVC
            saleRecordVC = SaleRecordVC()
            self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
        } else if indexPath.section == 3 && indexPath.row == 2 {
            presentVC = .suobanRecordVC
            suobanRecordVC = SuoBanVC()
            self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
        } else if indexPath.section == 3 && indexPath.row == 3 {
            presentVC = .dingyueRecordVC
            dingyueRecordVC = DingYueVC()
            self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
        } else if indexPath.section == 4 { //授信
            if indexPath.row == 0 {
                presentVC = .shouxinVC
                shouxinVC = ShouXinVC()
                self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
            } else if indexPath.row == 1 || indexPath.row == 2 {
                presentVC = .salerShouXinRecordVC
                shouxinRecordVC = ShouXinRecordVC()
                shouxinRecordVC.type = indexPath.row
                self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
            } else {
                presentVC = .indebtRecordVC
                indebtRecordVC = IndebtRecordVC()
                self.revealViewController().pushFrontViewController(revealViewController().frontViewController, animated: true)
            }
        }
    }
}

extension MenuVC: UserProfileDelegate {
    
    
    func imageDidChanged() {
        print("didChanged")
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let filePath = documentsURL.appendingPathComponent("icon.png").path
        DispatchQueue.main.async {
            let indexPath = IndexPath(row: 0, section: 0)
            let cell = self.tableView.cellForRow(at: indexPath) as! MenuCell
            //let cell = tableView.dequeueReusableCell(withIdentifier: "TopMenuCell") as! MenuCell
            cell.avargView.image = UIImage(contentsOfFile: filePath)
        }
    }
    func nameDidChanged() {
        let indexPath = IndexPath(row: 0, section: 0)
        let cell = self.tableView.cellForRow(at: indexPath) as! MenuCell
        cell.label.text = User.userName
    }
}

extension MenuVC: SWRevealViewControllerDelegate {
    func revealController(_ revealController: SWRevealViewController!, didAdd viewController: UIViewController!, for operation: SWRevealControllerOperation, animated: Bool) {
        print("interesting")
        switch presentVC {
        case .userProfileVC:
            (viewController as! UINavigationController).pushViewController(userProfileVC, animated: true)
        case .memberDetailVC:
            (viewController as! UINavigationController).pushViewController(memberDetailVC, animated: true)
        case .storeDetailVC:
            (viewController as! UINavigationController).pushViewController(storeDetailVC, animated: true)
        case .rukuRecordVC:
            (viewController as! UINavigationController).pushViewController(rukuRecordVC, animated: true)
        case .saleRecordVC:
            (viewController as! UINavigationController).pushViewController(saleRecordVC, animated: true)
        case .suobanRecordVC:
            (viewController as! UINavigationController).pushViewController(suobanRecordVC, animated: true)
        case .dingyueRecordVC:
            (viewController as! UINavigationController).pushViewController(dingyueRecordVC, animated: true)
        case .shoppingListVC:
            (viewController as! UINavigationController).pushViewController(shoppingListVC, animated: true)
        case .shouxinVC:
            (viewController as! UINavigationController).pushViewController(shouxinVC, animated: true)
        case .salerShouXinRecordVC:
            (viewController as! UINavigationController).pushViewController(shouxinRecordVC, animated: true)
        case .buyerShouXinRecordVC:
            (viewController as! UINavigationController).pushViewController(shouxinRecordVC, animated: true)
        case .indebtRecordVC:
            (viewController as! UINavigationController).pushViewController(indebtRecordVC, animated: true)
        case .none:
            break
        }
    }
}
